/*
 * 软件版权: Mr.LEE
 * 修改记录:
 * 修改日期     修改人员  修改说明
 * ========    =======  ============================================
 * 2020/12/24  LJH  新增
 * ========    =======  ============================================
 */
package com.lagou.cloud.controller;

import com.lagou.cloud.pojo.LagouAuthCode;
import com.lagou.cloud.service.AuthTokenService;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 功能说明:
 *
 * @author LJH
 */
@RestController
@RequestMapping(value = "/auth-token")
public class AuthTokenController {

    @Autowired
    private AuthTokenService authTokenService;

    @RequestMapping(value = "/get")
    public String generateAuthToken(@RequestParam("email") String email) {
        LagouAuthCode lagouAuthCode = new LagouAuthCode();
        lagouAuthCode.setEmail(email);
        int token = (int) ((Math.random() * 9 + 1) * 100000);
        lagouAuthCode.setCode(String.valueOf(token));
        Date now = new Date();
        Date expireTime = DateUtils.addMinutes(now, 10);
        lagouAuthCode.setCreateTime(now);
        lagouAuthCode.setExpireTime(expireTime);
        boolean result = authTokenService.addAuthToken(lagouAuthCode);
        if (result) {
            return lagouAuthCode.getCode();
        } else {
            return "fail";
        }
    }
}
